require(["../../conf/config.js"],function(){
	require(["jquery","common"],function($,common){
		//声明一个空数组储存每个输入框的结果
		let results = [];
		let inputs = Array.from(document.getElementsByTagName("input"));
		console.log(inputs);
		
		//每个输入框获得焦点的时候，显示span格式提示
		for(i=0;i<6;i++){
			inputs[i].onfocus = function(){
				this.parentNode.lastElementChild.style.display = "none";
				this.parentNode.lastElementChild.previousElementSibling	.className = "trueSpan";
				this.parentNode.lastElementChild.previousElementSibling	.style.display = "inline-block";
			};
			inputs[i].onblur = function(){
				this.parentNode.lastElementChild.previousElementSibling	.style.display = "none";
			};
		}
		
		//手机号验证
		inputs[0].addEventListener("blur", function(){
			if(/^1[0-9]{10}$/.test(this.value)) {
				results[0]=true;
			} else {
				results[0]=false;
				this.parentNode.lastElementChild.className = "errorSpan";
				this.parentNode.lastElementChild.style.display = "inline-block";
			}
		})
		//验证码
		inputs[1].addEventListener("blur", function(){
			if(/^[0-9a-zA-Z]{4}$/.test(this.value)) {
				results[1]=true;
			} else {
				results[1]=false;
				this.parentNode.lastElementChild.className = "errorSpan";
				this.parentNode.lastElementChild.style.display = "inline-block";
			}
		})
		//短信验证
		inputs[2].addEventListener("blur", function(){
			if(/^[0-9]{6}$/.test(this.value)) {
				results[2]=true;
			} else {
				results[2]=false;
				this.parentNode.lastElementChild.className = "errorSpan";
				this.parentNode.lastElementChild.style.display = "inline-block";
			}
		})
		//登录密码验证
		inputs[3].addEventListener("blur", function(){
			if(/^.{6,16}$/.test(this.value)) {
				results[3]=true;
			} else {
				results[3]=false;
				this.parentNode.lastElementChild.className = "errorSpan";
				this.parentNode.lastElementChild.style.display = "inline-block";
			}
		})	
		//确认密码验证
		inputs[4].addEventListener("blur", function(){
			if(this.value == inputs[3].value) {
				results[4]=true;	
			} else {
				results[4]=false;
				this.parentNode.lastElementChild.className = "errorSpan";
				this.parentNode.lastElementChild.style.display = "inline-block";
			}
		})
		//用户名验证
		inputs[5].addEventListener("blur", function(){
			if(/^\w{4,20}$/.test(this.value)) {
				results[5]=true;
			} else {
				results[5]=false;
				this.parentNode.lastElementChild.className = "errorSpan";
				this.parentNode.lastElementChild.style.display = "inline-block";
			}
		})
		
		
		
		//得到复选框元素
		var $cr = $("#cr");	
		
		//注册信息提交
		$("#registerBtn").click(function(){
			//在每次提交之前判断复选框有没有被选中
			if ($cr.is(":checked")){
				results[6]=true;
			}else{
				results[6]=false;
			}	
			console.log(results);		
			var res = results.every(function(item){
				return item===true;
			});
			if(res) {
				submit();
			} else {
				alert("用户信息不完全或没有勾选用户协议！");
			}
		})
		
		
		
		//注册提交cookie封装函数
		function submit(){
			var username = inputs[5].value;
			var password = inputs[4].value;
			var phone = inputs[0].value;
			var status = 0;
			var shopcar = [];
			
			var message = {phone,password,username,status,shopcar};
			
			var cookiestr = getCookie("userlist");
			//检测cookie里是否有userlist
			if(!cookiestr) {
				var arr = [];
			} else {
				arr = JSON.parse(cookiestr);
			}
			//检测是否存在该用户，在此使用手机号作为每个用户的唯一标志
			var exsit = false;
			
			arr.forEach(function(item){
				if(item.phone == message.phone) {
					exsit = true;
				}
			})
			
			if(!exsit) {
				arr.push(message);
				alert("注册成功!点击确定跳转到登录界面");
				setTimeout(function(){
					window.location.replace("http://localhost:8000/pages/login/login.html")
				},500);
				
			}else{
				alert("该手机号已被使用！")
			}
			setCookie("userlist", JSON.stringify(arr), 20);
		}
			
		//*****************************************
		
		
	})
})